package com.zisuyi.uilibrary.module.activty.home

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.weilele.mvvm.base.helper.appCompatActivity
import com.weilele.mvvm.gson
import com.weilele.mvvm.logI
import com.weilele.mvvm.utils.activity.navigateTo
import com.weilele.mvvm.utils.activity.onClick
import com.zisuyi.game.sdk.base.net.ApiInstance
import com.zisuyi.game.sdk.game.GameActivity
import com.zisuyi.game.sdk.game.GameEgret
import com.zisuyi.game.sdk.game.GameReceiveMessage
import com.zisuyi.game.sdk.game.bean.AndroidJsBean
import com.zisuyi.game.sdk.game.bean.GameNetworkApiBean
import com.zisuyi.game.sdk.library.login.IGameLogin
import com.zisuyi.game.sdk.module.HttpActivity
import com.zisuyi.game.sdk.net.GameApi
import com.zisuyi.game.sdk.net.GameModel
import com.zisuyi.uilibrary.R
import com.zisuyi.uilibrary.base.BaseUiActivity
import com.zisuyi.uilibrary.databinding.ActivityHomeBinding
import com.zisuyi.uilibrary.module.activty.redeem.RedeemActivity


class HomeActivity : BaseUiActivity<ActivityHomeBinding>() {
    companion object {
        fun start(appCompatActivity: AppCompatActivity?) {
            appCompatActivity.navigateTo<HomeActivity>()
            appCompatActivity?.finish()
        }
    }

    private val testMessage by lazy { GameReceiveMessage(GameEgret(this)) }
    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.tvSetting.onClick {
            testMessage.distributeMessages(
//                testVideoAd()
//                testHttp()
                message(
                    GameReceiveMessage.TYPE_SHOW_SETTING_DIALOG,
                    null
                )
            )
        }
        mBinding.tvToken.onClick {
            lifecycleScope.launchWhenCreated {
                logI(gson.toJson(GameModel.getGameInfo()))
            }
            /*  testMessage.distributeMessages(
                  message(
                      GameReceiveMessage.TYPE_GET_TOKEN,
                      null
                  )
              )*/
        }
        mBinding.tvRedeem.onClick {
            RedeemActivity.start(appCompatActivity)
        }
        mBinding.tvGame.onClick {
            GameActivity.start(appCompatActivity)
        }
        mBinding.tvHttp.onClick {
            HttpActivity.start(appCompatActivity)
        }
    }


    private fun message(type: String, json: String?): String {
        return gson.toJson(AndroidJsBean().apply {
            this.type = type
            this.json = json
        })
    }

    private fun testHttp(): String {
        val bean = AndroidJsBean()
        bean.type = GameReceiveMessage.TYPE_HTTP_API
        val apiBean = GameNetworkApiBean()
        apiBean.api = "/v2/user/"
        apiBean.params = gson.toJson(IGameLogin.lastUserInfo())
        bean.json = gson.toJson(apiBean)
        return gson.toJson(bean)
    }

    private fun testVideoAd(): String {
        val bean = AndroidJsBean()
        bean.type = GameReceiveMessage.TYPE_SHOW_VIDEO_AD
        bean.json = "true"
        return gson.toJson(bean)
    }
}